package com.event.management.backend.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.event.management.backend.dto.*;
import com.event.management.backend.service.ReportService;
import com.event.management.backend.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报表控制器
 */
@Api(tags = "报表统计接口")
@RestController
@RequestMapping("/api/reports")
public class ReportController {
    
    @Autowired
    private ReportService reportService;
    
    /**
     * 获取全站概况统计
     * @return 全站概况统计数据
     */
    @ApiOperation(value = "全站概况统计", notes = "管理员查看全站总览（报名/票务/活跃等）")
    @GetMapping("/overview")
    @SaCheckRole("admin")
    public Result<ReportOverviewDTO> getSystemOverview() {
        ReportOverviewDTO overview = reportService.getSystemOverview();
        return Result.success(overview);
    }
    
    /**
     * 获取活动销售统计
     * @param eventId 活动ID
     * @return 活动销售统计数据
     */
    @ApiOperation(value = "活动销售统计", notes = "查看单个活动销售、报名、问卷数据")
    @GetMapping("/events/{eventId}")
    @SaCheckRole(value = {"admin", "organizer"}, mode = SaMode.OR)
    public Result<EventReportDTO> getEventReport(
            @ApiParam(value = "活动ID", required = true) @PathVariable Long eventId) {
        // 检查权限
        if (!reportService.checkReportViewPermission(eventId)) {
            return Result.error(403, "您没有权限查看此活动的报表数据");
        }
        
        EventReportDTO report = reportService.getEventReport(eventId);
        return Result.success(report);
    }
    
    /**
     * 获取销售趋势图表
     * @param eventId 活动ID
     * @param range 时间范围
     * @param startDate 开始日期（自定义范围时使用）
     * @param endDate 结束日期（自定义范围时使用）
     * @return 销售趋势数据
     */
    @ApiOperation(value = "销售趋势图表", notes = "销售趋势（按日/周/小时）")
    @GetMapping("/events/{eventId}/sales-trend")
    @SaCheckRole(value = {"admin", "organizer"}, mode = SaMode.OR)
    public Result<SalesTrendDTO> getSalesTrend(
            @ApiParam(value = "活动ID", required = true) @PathVariable Long eventId,
            @ApiParam(value = "时间范围", example = "7d/30d/custom", defaultValue = "7d") 
            @RequestParam(defaultValue = "7d") String range,
            @ApiParam(value = "开始日期", example = "2023-05-01") 
            @RequestParam(required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2023-05-31") 
            @RequestParam(required = false) String endDate) {
        // 检查权限
        if (!reportService.checkReportViewPermission(eventId)) {
            return Result.error(403, "您没有权限查看此活动的报表数据");
        }
        
        SalesTrendDTO trend = reportService.getSalesTrend(eventId, range, startDate, endDate);
        return Result.success(trend);
    }
    
    /**
     * 获取问卷反馈概览
     * @param eventId 活动ID
     * @return 问卷反馈概览数据
     */
    @ApiOperation(value = "问卷反馈概览", notes = "活动反馈统计（满意度、得分等）")
    @GetMapping("/events/{eventId}/feedback")
    @SaCheckRole(value = {"admin", "organizer"}, mode = SaMode.OR)
    public Result<FeedbackReportDTO> getFeedbackOverview(
            @ApiParam(value = "活动ID", required = true) @PathVariable Long eventId) {
        // 检查权限
        if (!reportService.checkReportViewPermission(eventId)) {
            return Result.error(403, "您没有权限查看此活动的反馈数据");
        }
        
        FeedbackReportDTO feedback = reportService.getFeedbackOverview(eventId);
        return Result.success(feedback);
    }
    
    /**
     * 获取活跃用户排行
     * @param top 获取前几名
     * @return 活跃用户列表
     */
    @ApiOperation(value = "活跃用户排行", notes = "管理员查看系统活跃用户Top榜")
    @GetMapping("/active-users")
    @SaCheckRole("admin")
    public Result<List<ActiveUserDTO>> getActiveUsers(
            @ApiParam(value = "获取前几名", defaultValue = "10") 
            @RequestParam(defaultValue = "10") Integer top) {
        List<ActiveUserDTO> activeUsers = reportService.getActiveUsers(top);
        return Result.success(activeUsers);
    }
} 